package cn.webrx;

import cn.webrx.shiro.MyRealm;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.subject.Subject;

/**
 * <p>功能描述: </p>
 *
 * @author webrx
 * @version 1.0
 * @date 2020-06-08 18:29
 */
public class Test {
    public static void main(String[] args) {
        MyRealm i = new MyRealm();
        HashedCredentialsMatcher hm = new HashedCredentialsMatcher("md5");
        hm.setHashIterations(10);
        hm.setHashAlgorithmName("md5");
        i.setCredentialsMatcher(hm);
        DefaultSecurityManager dsm = new DefaultSecurityManager(i);
        SecurityUtils.setSecurityManager(dsm);
        Subject su = SecurityUtils.getSubject();
        String user = "webrx";
        String passwd = "123"; //密码
        UsernamePasswordToken token = new UsernamePasswordToken(user,passwd);
        try {
            su.login(token);
            System.out.println(su.isAuthenticated());
            su.login(token);
            System.out.println(su.isAuthenticated());
        }catch(IncorrectCredentialsException ice){
            System.out.println("密码错误");
        }catch(Exception e){
            e.printStackTrace();
        }


    }


    public static void md5(String[] args) {
        String user = "webrx";
        String passwd = "123";
        //5658b50772e0de0519d9b8c0199da7d9
        System.out.println(new Md5Hash(passwd,"wx",10));

        Object result = new SimpleHash("md5","123","wx",10);
        System.out.println(result);
    }
}
